
var sectionResize = {
    init: function() {
        $(window).resize(function(){
            sectionResize.resize();
        });
        this.resize();
    },

    resize: function() {
        $.each($('.multiSection'), function() {
            var contentText = $(this).find('.contentText');
            var contentTall = $(this).find('.contentTall');
            var hasSpecialClass = contentText.hasClass('specialText');
            contentText.height((contentTall.height()/2) - ((hasSpecialClass) ? 6 : 4));
        });
    }
};

var unlockArt = (function () {
    var self,
        firstRegionItem,
        firstPartnerItem,
        selectedRegion;

    self = {
        initialize: function () {
            firstRegionItem = $('#unlockArtRegion option:first').val();
            firstPartnerItem = $('#unlockArtPartner option:first').val();

            self.selectRegion();
            self.selectPartner();
        },
        selectRegion: function () {
            $("select#unlockArtRegion").change(function () {
                selectedRegion = $('select#unlockArtRegion').find("option:selected").val();

                $('#unlockArtPartner').val(firstPartnerItem);   // Resetting Partner Value on Region Change
                if (selectedRegion !== firstRegionItem) {
                    $('#unlockArtPartner').removeAttr('disabled');
                    self.getInstitutions();
                } else {
                    $('#unlockArtPartner').attr('disabled', 'disabled');
                }
            });
        },
        selectPartner: function () {
            $('#unlockArtPartner').change(function () {
                $('#unlockArtPartnerSelector').find('input[name="id"]').val($('#unlockArtPartner').find("option:selected").val());
                $('#unlockArtPartnerSelector').submit();
            });
        },
        getInstitutions: function () {
            $.ajax({
                url: "/ajax/unlockArtNav.do?region=" + selectedRegion,
                success: function (data) {
                    self.updateSelectableInstitutions(data);
                }
            });
        },
        updateSelectableInstitutions: function (data) {
            var html = '<option>' + firstPartnerItem + '</option>';
            $.each(data.data, function (arrayIndex, entry) {
                html += '<option value="' + entry.key + '">' + entry.title + '</option>';
            });
            $('#unlockArtPartner').html(html);
        }
    };
    return self;
})();

var goToSelectedPage = {
    init:function() {
        this.goToPage();
    },
    goToPage:function(){
        $("select#pageSelector").change(function () {
            parent.location = $('select#pageSelector').find("option:selected").val();
        });
    }
};

var bloodyMary = (function () {
	var self,
    selectCountry,
    selectHotel;

    self = {
		initialize: function () {
            self.selectCountry();
            self.selectHotel();
        },
	    selectCountry: function() {
	        $("select#bloodyMaryCountry").change(function () {
	            var selectedCountry = $('select#bloodyMaryCountry').find("option:selected").val(),
	                firstCountry = $('#bloodyMaryCountry option:first').val(),
	                firstHotel = $('#bloodyMaryProperty option:first').val(),
	                propertyOptions = $('#bloodyMaryPropertyOptions').html();
	            
	            $('#bloodyMaryProperty').val(firstHotel);   // Resetting Hotel Value on Country Change
	            if (selectedCountry !== firstCountry) {
	                $('#bloodyMaryProperty').html(propertyOptions);
	                $('#bloodyMaryProperty').removeAttr('disabled');
	                $('#bloodyMaryProperty option[value!=""]').not('option.' + selectedCountry).remove();
	            } else {
	                $('#bloodyMaryProperty').attr('disabled', 'disabled');
	                
	            }
	        });
	    },
	    selectHotel: function() {
	        $('#bloodyMaryProperty').change(function() {
	            var selectedPropertyId = $('#bloodyMaryProperty').find("option:selected").val();
	            var url = $('#bloodyMaryPropertySelector').attr('data-url')+selectedPropertyId;
	            if ( selectedPropertyId != ''){
		        	$('#bloodyMaryPropertySelector').attr('href', url);
		            $('#bloodyMaryPropertySelector').click();
	            }
	        });
	    }
    };
    return self;
})();

$(window).load(function(){
    sectionResize.init();
});

$(document).ready(function() {
    unlockArt.initialize();
    bloodyMary.initialize();
    goToSelectedPage.init();

    $.each($('.deleteContent'), function() {
        $(this).html('');
    });
});